<?php

	class VTYPE
	{
		const STRING = 0;
		const NUMERIC = 1;
		const INTEGER = 2;
		const FLOAT = 3;
		const DOUBLE = 4;
	} 
	
	
	
	function& ValidationItem( $name, $loc, $mandatory, $handler = null, $validation = null, $message = '', 
							  $size = null, $allow_empty = false, $type = null, $func = null )
	{
		if( func_num_args() == 3 )
		{
			$re = array(
				"exp" => $name,
				'handler' => $loc,
				'message' => $mandatory
			);
			
			return $re;
		}
		
		if( $allow_empty == null )
			$allow_empty == false;
			
		if( $type == null)
			$type = VTYPE::STRING;
		
		$re = array(
			'name' => $name,
			'loc' => $loc,
			'mandatory' => $mandatory,
			'handler' => $handler,
			'validation' => $validation,
			'message' => $message,
			'size' => $size,
			'type' => $type,
			'allow_empty' => $allow_empty,
			"filter" => $func
		);
		
		return $re;
	}
	
		
	function& ValidationVector( $precondition )
	{
		$numargs = func_num_args();
		$vitems = array();
		
		for( $i = 1; $i < $numargs; $i++)
			$vitems[] = func_get_arg($i);
			
		$re = array(
			"pre" => $precondition,
			"vitem" => $vitems
		);
		
		return $re;
	}
	
	function& ValidationPage()
	{
		$numargs = func_num_args();
		$pageitems = array();
		
		for( $i = 0; $i < $numargs; $i++)
			$pageitems[] = func_get_arg($i);
			
		return $pageitems;
	}
	
	/**********************************************************
	 *	All the regular expression for input field
	 **********************************************************/
	define( "REG_ID", '/^[1-9]\d*$/' );
	define( "REG_EMAIL", "/^[_.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+.)+[a-zA-Z]{2,3}$/" );

	
	
	/**********************************************************
	 *	Vlidation definition for pages
	 **********************************************************/
	
	
	/**********************************************************
	 *	add_review
	 **********************************************************/
	$i_sid = ValidationItem( 'sid', 'REQUEST', 1, ILLEGAL_ACCESS, REG_ID );
	$i_pid = ValidationItem( 'pid', 'REQUEST', 1, ILLEGAL_ACCESS, REG_ID );
	$i_product_page = ValidationItem( 'product_page', 'REQUEST', 1, ABORT_EXECUTE, "/^1$/" );
	
	$i_score = ValidationItem( 'score', 'POST', 1, CONTINUE_EXECUTE, "/^[0-4]$/", '请选择总体评分' );
	$i_service = ValidationItem( 'service', 'POST', 1, CONTINUE_EXECUTE, "/^[0-4]$/", '请选择服务评分' );
	$i_velocity = ValidationItem( 'velocity', 'POST', 1, CONTINUE_EXECUTE, "/^[0-4]$/", '请选择真实度评分' );
	$i_reality = ValidationItem( 'reality', 'POST', 1, CONTINUE_EXECUTE, "/^[0-4]$/", '请选择发货速度评分' );
	$i_rate = ValidationItem( 'rate', 'POST', 1, CONTINUE_EXECUTE, "/^[0-4]$/", '请选择性价比评分' );
	$i_content = ValidationItem( 'content', 'POST', 1, CONTINUE_EXECUTE, null, '评论的内容不能为空，或者长度超出最大限制!',
		array(1, MAX_REVIEW_CONTENT), false);
	$i_content2 = ValidationItem( 'content', 'POST', 1, CONTINUE_EXECUTE, null, "评论内容包含了非法的单词", null, false, null, "EdpFilterSensitiveWords" );
	$i_files = ValidationItem( 'files', 'FILES', 0, CONTINUE_EXECUTE, null, "不能同时传超过5张图片！", array(0, 5), true );
	$i_product_id_list = ValidationItem( 'product_lists', 'POST', 0, CONTINUE_EXECUTE, "/^([1-9]\d*,)([1-9]\d*,){0,9}$/", "一个评论中不允许添加超过10件商品", null, true );
        $i_paymethod = ValidationItem( 'paymethod', 'POST', 0, CONTINUE_EXECUTE, "/^\d{1}$/", "最多选择10种支付方式", array(1, 10) );
	$i_feature = ValidationItem( 'feature', 'POST', 0, CONTINUE_EXECUTE, "/^\d{1,2}$/",  "最多选择10种特色", array(1, 10) );
	$i_tag = ValidationItem( 'tag', 'POST', 1, CONTINUE_EXECUTE, "/^([0-9a-zA-Z ]|[\x{4e00}-\x{9fa5}])+$/u", "标签只能是字母、数字或者中文，不能超过200个字符", array(1, 200), true );
	$i_pic_name = ValidationItem( 'picnames', 'POST', 0, CONTINUE_EXECUTE, "/^([0-9a-zA-Z_\.]|[\x{4e00}-\x{9fa5}]){1,50}$/u", "文件名称不能超过50个字符，只能是中文、数字、大小写字母和下划线", array(0, 5), true );

        $i_vcode = ValidationItem('vcode', 'POST', 0, CONTINUE_EXECUTE,null, "验证码不正确", null, false, null, "EdpCheckVcode" );

	$v_add_review_sid = ValidationVector( null, $i_sid );
	$v_add_review_pid = ValidationVector( 'isset($_REQUEST["product_page"])' ,$i_pid, $i_product_page);
	$v_add_review_main = ValidationVector( 'isset($_POST["action"])', 
		$i_score, $i_service, $i_velocity, $i_reality, $i_rate, $i_content, $i_files, $i_product_id_list, $i_paymethod, $i_feature, $i_tag,
		$i_pic_name, $i_content2, $i_vcode);


	$v_add_review = ValidationPage( $v_add_review_sid, $v_add_review_pid, $v_add_review_main );
	
	/**********************************************************
	 *	Register
	 **********************************************************/
	
	$i_nickname = ValidationItem( 'nick', 'POST', 1, CONTINUE_EXECUTE, "/^([0-9a-zA-Z_]|[\x{4e00}-\x{9fa5}])+$/u", "用户名必须为大小写、下划线或中文字符组成的不超过40个长度的字符串", array(1, 20) );
	$i_nickname2 = ValidationItem( 'nick', 'POST', 1, CONTINUE_EXECUTE, null, "用户名包含了非法的单词", null, false,null, "EdpFilterSensitiveWords" );
	$i_email = ValidationItem( 'email', 'POST', 1, CONTINUE_EXECUTE, REG_EMAIL, "EMAIL 格式不正确", array(1, 50) );
	$i_password = ValidationItem( 'pass', 'POST', 1, CONTINUE_EXECUTE, null, "密码为6至20个字符", array(6, 20) );
	
	$v_reg_info =  ValidationVector( 'isset($_POST["action"])', $i_nickname,$i_nickname2, $i_email, $i_password );
	
	$v_register = ValidationPage( $v_reg_info );
	
	/**********************************************************
	 *	user_settings
	 **********************************************************/
	
	$i_id = ValidationItem( 'id', 'POST', 1, ILLEGAL_ACCESS, REG_ID );
	$i_location = ValidationItem( 'location', 'POST', 1, ILLEGAL_ACCESS, "/^\d+$/");
	//$i_introduction = ValidationItem( 'introduction', 'POST', 1, CONTINUE_EXECUTE, null, "用户简介只能在300个字符以内", array(1, 300), true );
	
	$i_sex = ValidationItem( 'sex', 'POST', 0, ILLEGAL_ACCESS, "/^[01]$/", null, null, true );
	$i_profession = ValidationItem( 'profession', 'POST', 0, ILLEGAL_ACCESS, "/^\d{1,3}$/", null, null, true );
	$i_edcation = ValidationItem( 'education', 'POST', 0, ILLEGAL_ACCESS, "/^\d$/", null, null, true );
	$i_salary = ValidationItem( 'salary', 'POST', 0, ILLEGAL_ACCESS, "/^\d$/", null, null, true );
	$i_qq = ValidationItem( 'qq', 'POST', 0, CONTINUE_EXECUTE, "/^[0-9]{5,10}$/", "QQ 号码格式不对!", null, true );
	$i_msn = ValidationItem( 'msn', 'POST', 0, CONTINUE_EXECUTE, "/^[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z]{2,3}$/", "MSN 格式不对", null, true );
	$i_shopping_method = ValidationItem( 'shopping_method', 'POST', 0, CONTINUE_EXECUTE, null, "购物渠道不能超过180个字符!", array(1, 180), true );
	$i_shopping_plat = ValidationItem( 'shopping_plat', 'POST', 0, CONTINUE_EXECUTE, null, "购物平台不能超过180个字符!", array(1, 180), true );
	$i_interested_cate = ValidationItem( 'interested_cate', 'POST', 0, CONTINUE_EXECUTE, null, "感兴趣商品不能超过180个字符!", array(1, 180), true );
	$i_interested_grand = ValidationItem( 'interested_grand', 'POST', 0, CONTINUE_EXECUTE, null, "喜欢品牌不能超过180个字符!", array(1, 180), true );
	
	$i_user_name = ValidationItem( 'name', 'POST', 0, CONTINUE_EXECUTE, "/^([0-9a-zA-Z ]|[\x{4e00}-\x{9fa5}])*$/u", "姓名必须为字母，空格或中文字符组成的不超过18个长度的字符串", array(1, 18), true );
	$i_user_email = ValidationItem( 'email', 'POST', 0, CONTINUE_EXECUTE, REG_EMAIL, "EMAIL 格式不正确", array(1, 50), true );
	$i_user_address = ValidationItem( 'address', 'POST', 0, CONTINUE_EXECUTE, null, "地址信息不能超过180个字符", array(1, 180), true );
	$i_user_zip = ValidationItem( 'zip', 'POST', 0, CONTINUE_EXECUTE, "/^\d{6,6}$/", "邮编格式不正确", array(6, 6), true );
	$i_user_phone = ValidationItem( 'phone', 'POST', 0, CONTINUE_EXECUTE, "/^(\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$/", "电话格式不正确", array(1, 18), true );
	$i_user_oldpwd = ValidationItem( 'oldpwd', 'POST', 0, CONTINUE_EXECUTE, null, "密码不能超过20个字符", array(1, 20), true );
	$i_user_newpwd = ValidationItem( 'newpwd', 'POST', 0, CONTINUE_EXECUTE, null, "密码不能超过20个字符", array(6, 20), true );
	$i_user_newpwdagain = ValidationItem( 'newpwdagain', 'POST', 0, CONTINUE_EXECUTE, null, "密码确认不正确", array(6, 20), true );
	
	$v_user_account_info = ValidationVector( 'isset($_POST["action"]) && $_POST["action"] == "account_info"',  
		$i_user_name, $i_user_email, $i_user_address, $i_user_zip, $i_user_phone, $i_user_oldpwd, $i_user_newpwd, $i_user_newpwdagain);
		
	
	
	$v_user_detail_info =  ValidationVector( 'isset($_POST["action"]) && $_POST["action"] == "detail_info"', 
		$i_sex, $i_profession, $i_edcation, $i_salary, $i_qq, $i_msn, $i_shopping_method, $i_shopping_plat, $i_interested_cate, $i_interested_grand );
	
	
	// the location should be added here later on
	$v_user_basic_info =  ValidationVector( 'isset($_POST["action"]) && $_POST["action"] == "basic_info"', $i_id );
	
	$v_user_setting = ValidationPage( $v_user_basic_info, $v_user_account_info, $v_user_detail_info );
	
	
	/**********************************************************
	 *	user list
	 **********************************************************/
	$i_list_id = ValidationItem( 'lid', 'REQUEST', 1, ILLEGAL_ACCESS, REG_ID );
	$i_list_title = ValidationItem( 'list_name', 'POST', 1, CONTINUE_EXECUTE, "/^([0-9a-zA-Z ]|[\x{4e00}-\x{9fa5}])*$/u", "评单名称不能超过100个英文字母或者50个中文字母", array(1, 100) );
	$i_list_desc = ValidationItem( 'list_desc', 'POST', 1, CONTINUE_EXECUTE, null, "评单描述不能超过1000个英文字母或者500个中文字母", array(1, 1000) );
	$i_list_desc2 = ValidationItem( 'list_desc', 'POST', 1, CONTINUE_EXECUTE, null, "评单内容包含了非法的单词", null, false,null, "EdpFilterSensitiveWords" );
	$i_list_label = ValidationItem( 'list_label', 'POST', 1, CONTINUE_EXECUTE, "/^([0-9a-zA-Z ]|[\x{4e00}-\x{9fa5}])*$/u", "评单标签最多不超过200个字符，只能是中文、数字、大小写字母", array(1, 200), true );
	
	//$i_list_content = ValidationItem( 'list_content', 'POST', 1, CONTINUE_EXECUTE, "/^((\d)+,)+$/", "必须添加至少一件商品或店铺！" );
	$i_list_priority = ValidationItem( 'list_priority', 'POST', 1, CONTINUE_EXECUTE, "/^((\d)+,)+$/" );
	$i_list_cate = ValidationItem( 'list_cate', 'POST', 1, CONTINUE_EXECUTE, "/^([012],)+$/" );
	
	$vc_list_create = ValidationVector( 'isset($_POST["action"])', $i_list_title, $i_list_desc, $i_list_desc2, $i_list_label, $i_list_priority, $i_list_cate );
	$vc_list_view = ValidationVector( 'isset($_GET["lid"])', $i_list_id );
	$vc_list_edit = ValidationVector( 'isset($_POST["action"]) && $_POST["action"] == "edit_list"', $i_list_id );
	
	$v_user_list_create = ValidationPage( $vc_list_create, $vc_list_view, $vc_list_edit );
	
	/**********************************************************
	 *	ajax_edit_list_entry
	 **********************************************************/
	$i_list_entry_content = ValidationItem( 'content', 'POST', 1, CONTINUE_EXECUTE, null, "评单推荐内容不能超过1000个英文字母或者500个中文字母", array(1, 1000) );
	
	$vc_edit_list_entry = ValidationVector( null, $i_list_id, $i_list_entry_content );
	
	$v_ajax_edit_list_entry = ValidationPage( $vc_edit_list_entry );
	
	/**********************************************************
	 *	mail box 
	 **********************************************************/	
	
	$i_receiver_id_list = ValidationItem( 'ids', 'POST', 1, CONTINUE_EXECUTE, "/^([1-9]\d{0,10},)*$/", "收件人不正确！", null );
	$i_letter_title = ValidationItem( 'title', 'POST', 1, CONTINUE_EXECUTE, null, "标题不能为空，也不能超过50个字符！", array(1, 50) );
	$i_letter_content = ValidationItem( 'content', 'POST', 1, CONTINUE_EXECUTE, null, "内容不能为空，也不能超过1000个字符！", array(1, 1000) );
	
	$i_letter_title_reply = ValidationItem( 'title', 'POST', 1, CONTINUE_EXECUTE, null, "标题不能为空，也不能超过50个字符！", array(1, 53) );
	$i_receiver_id_list_reply = ValidationItem( 'id', 'POST', 1, CONTINUE_EXECUTE, "/^[1-9]\d*$/", "收件人不正确, 不能向管理员回复信件！", null );
	
	$i_send_mail = ValidationVector( 'isset($_POST["action"]) && $_POST["action"] == "send"', 
		$i_receiver_id_list, $i_letter_title, $i_letter_content );
		
	$i_reply_mail = ValidationVector( null, $i_letter_title_reply, $i_receiver_id_list_reply, $i_letter_content );
		
	$v_write_message = ValidationPage( $i_send_mail ); 
	$v_ajax_reply_mail = ValidationPage( $i_reply_mail );
	
	/**********************************************************
	 *		Add shop
	 **********************************************************/
	$i_shop_url = ValidationItem( 'url', 'POST', 1, CONTINUE_EXECUTE, '/http:\/\/\S*/',  "店铺URL不正确！", array(1, 200) );
	$i_shop_vcode = ValidationItem( '$_POST["vcode"] == edp_getsession( "vcode" )', CONTINUE_EXECUTE, "验证码错误！" );
	
	
	
	$i_shop_name = ValidationItem( 'shopName', 'POST', 1, CONTINUE_EXECUTE, '/^[^<>]+$/',  "店铺名称不能超过200个字符,不能包含HTML标签和<>符号！", array(1, 200) );
	$i_shop_nick = ValidationItem( 'nickName', 'POST', 1, CONTINUE_EXECUTE, null,  "店铺主昵称不正确！", array(1, 40) );
	//$i_shop_desc = ValidationItem( 'shopDesc', 'POST', 1, CONTINUE_EXECUTE, null,  "店铺描述不能超过5000个字符！", array(1, 5000),true );
	$i_shop_create_date = ValidationItem( 'createdDate', 'POST', 1, CONTINUE_EXECUTE, '/[0-9]{4}-[0-9]{2}-[0-9]{2}/',  "创建日期不正确！", array(1, 10),true );//日期是加类型还是用正则表示？
	$i_shop_logo_url = ValidationItem( 'logoURL', 'POST', 1, CONTINUE_EXECUTE, null,  "logo地址长度不正确！", array(1, 200),true );
	//$i_shop_location = ValidationItem( 'shopLocation', 'POST', 1, CONTINUE_EXECUTE, null,  "店铺地址不正确！", array(1, 492), null, VTYPE::INTEGER );
	$i_shop_category = ValidationItem( 'shopCategory', 'POST', 1, CONTINUE_EXECUTE, null,  "店铺类别不正确！", array(1, 53), null, VTYPE::INTEGER );
	
	$i_add_shop1 = ValidationVector( 'isset($_POST["action"]) && $_POST["action"]=="addShop1"', $i_shop_url,$i_shop_vcode );
	$i_add_shop2 = ValidationVector( 'isset($_POST["action"]) && $_POST["action"]=="addShop2"', $i_shop_name, $i_shop_nick, $i_shop_create_date,
		 $i_shop_logo_url, $i_shop_category );


		
	$v_add_shop = ValidationPage( $i_add_shop1, $i_add_shop2 );
	
	/**********************************************************
	 *		Add product
	 **********************************************************/
	$i_product_url = ValidationItem( 'url', 'POST', 1, CONTINUE_EXECUTE, '/http:\/\/\S*/',  "商品URL不正确！", array(1, 300) );
	$i_product_sid = ValidationItem( 'sid', 'POST', 1, CONTINUE_EXECUTE, null, "没有相应的店铺", null, null, VTYPE::NUMERIC );
	$i_product_vcode = ValidationItem( '$_POST["vcode"] == edp_getsession( "vcode" )', CONTINUE_EXECUTE, "验证码错误！" );
	
	
	$i_product_name = ValidationItem( 'productName', 'POST', 1, CONTINUE_EXECUTE, '/^[^<>]+$/',  "商品名称不能超过200个字符，,不能包含HTML标签和<>符号！", array(1, 200) );
	$i_product_price = ValidationItem( 'productPrice', 'POST', 1, CONTINUE_EXECUTE, null,  "商品价格不正确！", null, null, VTYPE::FLOAT );
	//$i_product_location = ValidationItem( 'productLocation', 'POST', 1, CONTINUE_EXECUTE, null,  "商品地址不正确！", array(1, 492), null, VTYPE::INTEGER );
//	$i_product_category = ValidationItem( 'productCategory', 'POST', 1, CONTINUE_EXECUTE, null,  "商品类别不正确！", array(1, 68), null, VTYPE::INTEGER );放到header_php里判断
	$i_product_picture = ValidationItem( 'productPicture', 'POST', 1, CONTINUE_EXECUTE, null,  "商品图像不正确！", array(1, 200) ,true);
//	$i_product_desc = ValidationItem( 'productDesc', 'POST', 1, CONTINUE_EXECUTE, null,  "商品描述不能超过50000个字符！", array(1, 50000),true );
	
	
	$i_add_product1 = ValidationVector( 'isset($_POST["action"]) && $_POST["action"]=="addProduct1"', $i_product_url, $i_product_sid, $i_product_vcode );
	
	$i_add_product2 = ValidationVector( 'isset($_POST["action"]) && $_POST["action"]=="addProduct2"', $i_product_name, $i_product_price, $i_product_picture );
		
	$v_add_product = ValidationPage( $i_add_product1, $i_add_product2);
	

	/**********************************************************
	 *		Modify shop
	 **********************************************************/
	$i_shop_vcode = ValidationItem( '$_POST["vcode"] == edp_getsession( "vcode" )', CONTINUE_EXECUTE, "验证码错误！" );

	$i_shop_name = ValidationItem( 'shopName', 'POST', 1, CONTINUE_EXECUTE, '/^[^<>]+$/',  "店铺名称不能超过200个字符,不能包含HTML标签和<>符号！", array(1, 200) );
	$i_shop_logo_url = ValidationItem( 'logoURL', 'POST', 1, CONTINUE_EXECUTE, null,  "logo地址长度不正确！", array(1, 200),true );
	
	$i_modify_shop = ValidationVector( 'isset($_POST["action"]) && $_POST["action"]=="updateShop"', $i_shop_name,$i_shop_logo_url,$i_shop_vcode );
		
	$v_modify_shop = ValidationPage( $i_modify_shop);	
	
	/**********************************************************
	 *		ajax_chang_name
	 **********************************************************/
	$i_acn_pid = ValidationItem( 'pid', 'POST', 1, ILLEGAL_ACCESS, REG_ID );
	$i_acn_name = ValidationItem( 'name', 'POST', 1, CONTINUE_EXECUTE, "/^([0-9a-zA-Z_\.]|[\x{4e00}-\x{9fa5}]){1,50}$/u", "文件名称不能超过50个字符，只能是中文、数字、大小写字母和下划线" );
	
	$i_acn = ValidationVector( null, $i_acn_pid, $i_acn_name );
	$v_ajax_change_name = ValidationPage( $i_acn );
	
	/**********************************************************
	 *		ajax_error_report
	 **********************************************************/
	$i_error_name = ValidationItem( 'errorName', 'POST', 1, CONTINUE_EXECUTE, "/^([0-9a-zA-Z_]|[\x{4e00}-\x{9fa5}]){1,50}$/u", "姓名不正确！" );
	$i_error_email = ValidationItem( 'errorEmail', 'POST', 1, CONTINUE_EXECUTE, REG_EMAIL, "邮件格式不正确" );
	$i_error_desc = ValidationItem( 'errorDescription', 'POST', 1, CONTINUE_EXECUTE, null, "报错内容不能为空或者超过1000个字符！", array(1, 2000) );
	$i_error = ValidationVector( 'isset($_POST["action"]) && $_POST["action"] == "addErrorReport"', $i_error_name, $i_error_email,$i_error_desc );
	$v_ajax_error_report = ValidationPage( $i_error );
	
	/**********************************************************
	 *		ajax_collect
	 **********************************************************/
	$i_collect_tags = ValidationItem( 'favTags', 'POST', 1, CONTINUE_EXECUTE, "/^([0-9a-zA-Z ]|[\x{4e00}-\x{9fa5}])+$/u", "标签不能超过200个字符，只能是字母、数字或者中文", array(1, 200), true );
	$i_collect = ValidationVector( '$_GET["tiny"] == 2', $i_collect_tags );
	$v_ajax_collect = ValidationPage( $i_collect );
	
?>
